package com.zzjson.jvm;

import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.jvm</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年12月12日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class MetaspaceUtil extends ClassLoader {

	public static List<Class<?>> createClasses() {
		List<Class<?>> classes = new ArrayList<Class<?>>();
		for (int i = 0; i < 10000000; ++i) {
			ClassWriter cw = new ClassWriter(0);
			cw.visit(Opcodes.V1_1, Opcodes.ACC_PUBLIC, "Class" + i, null,
					"java/lang/Object", null);
			MethodVisitor mw = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>",
					"()V", null, null);
			mw.visitVarInsn(Opcodes.ALOAD, 0);
			mw.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object",
					"<init>", "()V");
			mw.visitInsn(Opcodes.RETURN);
			mw.visitMaxs(1, 1);
			mw.visitEnd();
			MetaspaceUtil test = new MetaspaceUtil();
			byte[] code = cw.toByteArray();
			Class<?> exampleClass = test.defineClass("Class" + i, code, 0, code.length);
			classes.add(exampleClass);
		}
		return classes;
	}
}
